package com.yihu.cms.action;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.yihu.article.beans.Article;
import com.yihu.beans.ResultVO;
import com.yihu.beans.dic.Config;
import com.yihu.beans.dic.DatadicQuery;
import com.yihu.cms.service.ArticleService;
import com.yihu.cms.service.DataDictionaryService;
import com.yihu.cms.util.CommonUtils;

@Controller
public class DataDicConfigController {
private final Logger logger = LoggerFactory.getLogger(DataDicConfigController.class); 
	DataDictionaryService service = new DataDictionaryService();
	
	@RequestMapping(value = "/datadic/config", method = RequestMethod.POST)
	public @ResponseBody Object add(@RequestBody Config config) {
		logger.debug("/datadic/config.add: "+config);
		ResultVO result = service.addDataConfig(config);
		return JSON.toJSONString(result);
	}
	
	@RequestMapping(value = "/datadic/config/{id}", method = RequestMethod.DELETE)
	public @ResponseBody Object delete(@PathVariable("id") String id) {
		logger.debug("/datadic/config.delete: "+id);
		Config config = new Config();
		config.setId(id);
		ResultVO result = service.deleteDataConfig(config);
		return JSON.toJSONString(result);
	}
	
	@RequestMapping(value = "/datadic/config/{id}", method = RequestMethod.GET)
	public @ResponseBody Object getConfig(@PathVariable("id") String id) {
		logger.debug("getConfig: "+id);
		Config config = new Config();
		config.setId(id);
		Object o = service.getDataConfig(config);
		return JSON.parse(JSON.toJSONString(o));
	}
	
	
	@RequestMapping(value = "/datadic/config/{id}", method = RequestMethod.PUT)
	public @ResponseBody Object update(@RequestBody Config config,@PathVariable("id") String id) {
		config.setId(id);
		logger.debug("/datadic/config.update: "+config);
		ResultVO result = service.updateDataConfig(config);
		return JSON.toJSONString(result);
	}
	
	@RequestMapping(value = "/datadic/configs", method = RequestMethod.GET)
	public @ResponseBody Object getConfigsByParent(DatadicQuery query) {
		logger.debug("getConfigsByParent: "+query);
		Object o = service.getDataConfigs(query);
		return JSON.parse(JSON.toJSONString(o));
	}
	
}
